﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SMS.DAL.Providers
{
    public class StockUsersProvider : ProviderBase<StockUser, StockUserSearchCriteria>
    {
        public override void Insert(StockUser item)
        {
            Context.StockUsers.AddObject(item);
            Context.SaveChanges();
        }

        public override void Update(StockUser updatedItem)
        {
            throw new NotImplementedException();
        }

        public override void Delete(int id)
        {
            Context.StockUsers.DeleteObject(this.GetByID(id));
            Context.SaveChanges();

        }
        public void Delete(int SID,int UID)
        {
            Context.StockUsers.DeleteObject((from u in Context.StockUsers where u.StockID==SID && u.UserID ==UID select u).SingleOrDefault());
            Context.SaveChanges();
        }

        public override StockUser GetByID(int id)
        {
            return (from c in Context.StockUsers
                    where c.ID == id
                    select c).SingleOrDefault();
        }

        public override List<StockUser> Search(StockUserSearchCriteria SearchCriteria)
        {
            var _stockusers = (from su in Context.StockUsers
                               where su.UserID == SearchCriteria.UserID
                               select su).ToList<StockUser>();
            if (_stockusers.Count > 0)
                return _stockusers;
            else
                return null;
        }

        public List<Stock> GetStocksByUserID(StockUserSearchCriteria SearchCriteria)
        {
            var quary = (from su in Context.StockUsers
                         where su.UserID == SearchCriteria.UserID
                         select su.Stock).ToList<Stock>();
            return quary;
        }

        public StockUser GetByUserIDandStockID(StockUserSearchCriteria search)
        {
            return (from q in Context.StockUsers
                    where ((q.UserID == search.UserID) && (q.StockID == search.StockID))
                    select q).SingleOrDefault();
        }
    }
}
